import csv
import re

input_file = 'XDHYCD7th.txt'
output_file = 'XDHYCD7th.csv'

word_list = []

with open(input_file, 'r', encoding='utf-8') as file:
    i=0
    for line in file:
        match = re.match(r'【(.*?)】(.+)', line.strip())
        if match:
            i+=1
            word = match.group(1)
            meaning = match.group(2).strip()
            if len(word)==1:
                continue
            else:
                word_list.append((word, meaning))

# 将词语和含义写入CSV文件
with open(output_file, 'w', encoding='utf-8', newline='') as csvfile:
    writer = csv.writer(csvfile)
    writer.writerow(['word', 'meaning'])  # 写入CSV文件的表头
    writer.writerows(word_list)  # 写入所有词语及其含义

print(f"成功将词典数据导出到 {output_file}")